package com.piz.web.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.piz.model.User;
import com.piz.service.common.UnexpectedServiceException;
import com.piz.service.userservice.UserService;


/**
 * @author prameetk.sharma
 *
 */
@Component("registrationValidator")
public class RegistrationValidation implements Validator {
	private static final String CHAR_ONLY = "^[a-zA-Z]+$";
	
	
	@Autowired
	UserService userService;
	
	@Autowired
	ContactValidator contactValidator;
	
	/* Get Support class
	 * @see org.springframework.validation.Validator#supports(java.lang.Class)
	 */
	public boolean supports(Class<?> klass) {
		return User.class.isAssignableFrom(klass);
	}

	/* Validate User Bean
	 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
	 */
	public void validate(Object target, Errors errors) {
		User user = (User) target;
		
		if(user.getFname() != null && !user.getFname().matches(CHAR_ONLY))
		{
			errors.rejectValue("fname", "", "Not a valid First Name. Please use only characters.");
		}
		if(user.getLname() != null && !user.getLname().matches(CHAR_ONLY))
		{
			errors.rejectValue("lname", "Not a valid Last Name. Please use only characters.");
		}
		try {
			if(user!=null && user.getUsername()!=null && userService.isUsernameExsist(user.getUsername()))
			{
				errors.rejectValue("username", "", "Username already exsist. Please try another one.");
			}
		} catch (UnexpectedServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (!errors.hasErrors() && !(user.getPassword()).equals(user
				.getCpassword())) {
			errors.rejectValue("password",
					"matchingPassword.registration.password",
					"Password and Confirm Password Not match.");
		}
		if(user!=null && user.getRegisterAs()!=null && user.getRegisterAs().equalsIgnoreCase("Company"))
		{
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "contact.designation", "errorCode", "Designation must not be Empty");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "contact.companyName", "errorCode", "Company Name must not be Empty");
		}
		try {
	          errors.pushNestedPath("contact");
	          ValidationUtils.invokeValidator(this.contactValidator, user.getContact(), errors);
	      } finally {
	          errors.popNestedPath();
	      }
		
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ContactValidator getContactValidator() {
		return contactValidator;
	}

	public void setContactValidator(ContactValidator contactValidator) {
		this.contactValidator = contactValidator;
	}
}
